package com.xz.queue;

import com.xz.bean.LinkReg;
import com.xz.http.instance.BerkeleyDBSingleton;
import com.xz.http.instance.ClientSingleton;
import com.xz.http.instance.HttpFactory;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * Created by YLZX-Z0107 on 2014/7/21.
 */
public class QueueStart {
    public static void main(String[] args) throws InterruptedException {
        BlockingQueue<String> queue = new LinkedBlockingQueue<String>();
        initQueue(queue);
        Producer producer = new Producer(queue);
        Producer producer1 = new Producer(queue);
        Producer producer2 = new Producer(queue);
        Producer producer3 = new Producer(queue);
        Producer producer4 = new Producer(queue);
        Producer producer5 = new Producer(queue);
        // 借助Executors
        ExecutorService service = Executors.newCachedThreadPool();
        // 启动线程
        service.execute(producer);
        sleep500();
        service.execute(producer1);
        sleep500();
        service.execute(producer2);
        sleep500();
        service.execute(producer3);
        sleep500();
        service.execute(producer4);
        sleep500();
        service.execute(producer5);
    }

    public static void setUri(HttpGet get, String uri) {
        try {
            get.setURI(new URI(uri));
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }

    private static void initQueue(BlockingQueue<String> queue) throws InterruptedException {
        CloseableHttpClient client = ClientSingleton.getInstance().getClient();
        HttpGet get = HttpFactory.getMethod();
        BerkeleyDBSingleton berkeley = BerkeleyDBSingleton.getInstance();
        get.addHeader("Cookie", "PHPSESSID=go95798enia09k0vcc6k9ao3n0");
        BlockingQueue<String> init = new LinkedBlockingQueue<String>();
        init.put("http://www.arzon.jp");
        init.put("http://www.arzon.jp/item_1208568.html");
        init.put("http://www.arzon.jp/item_615011.html");
        init.put("http://www.arzon.jp/item_1188458.html");
        init.put("http://www.arzon.jp/item_1208875.html");
        init.put("http://www.arzon.jp/item_1181154.html");
        init.put("http://www.arzon.jp/item_1050599.html");
        init.put("http://www.arzon.jp/item_523239.html");
        init.put("http://www.arzon.jp/item_947401.html");
        init.put("http://www.arzon.jp/item_710259.html");
        init.put("http://www.arzon.jp/item_1002923.html");
        while(!init.isEmpty()) {
            setUri(get, init.take());
            String content = getResponseContent(client, get);
            Document document = Jsoup.parse(content);
            Elements elements = document.select("a[href]");
            LinkReg reg = new LinkReg();
            reg.setLinkCon("item_");
            reg.setLinkUnCon("mailto");
            for (Element element : elements) {
                if (isCorrect(element, reg)) {
                    String detail = element.attr("href");
                    if (berkeley.has(getAbsUrl(detail))) {
                        queue.put(getAbsUrl(detail));
                    }
                }
            }
        }
    }

    private static boolean isCorrect(Element element, LinkReg reg) {
        boolean cur = true;
        if (reg != null && reg.getLinkUnCon() != null) {
            String[] linkUnCon = reg.getLinkUnCon().split(",");
            for (String link : linkUnCon) {
                if (element.attr("href").contains(link)) {
                    return false;
                }
            }
        }
        if (reg != null && reg.getLinkCon() != null) {
            String[] linkCon = reg.getLinkCon().split(",");
            for (String link : linkCon) {
                if (!element.attr("href").contains(link)) {
                    return false;
                }
            }
        }
        if (StringUtils.isEmpty(element.text())) {
            return false;
        }
        return cur;
    }

    public static String getAbsUrl(String url) {
        if (url.indexOf("/") == 0) {
            return "http://www.arzon.jp" + url;
        }
        return url;
    }

    public static String getResponseContent(CloseableHttpClient client, HttpRequestBase method) {
        String result = null;
        try {
            CloseableHttpResponse response = client.execute(method);
            result = EntityUtils.toString(response.getEntity(), "utf-8");
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                System.out.println("get success!");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    private static void sleep500() {
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
